package ext.tianma.part.statute;

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import ext.tianma.bom.util.StringUtil;
import ext.tianma.util.IbaAttributeUtil;
import wt.fc.WTObject;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.util.WTException;

/**
 * 用于物料根据物料的中英文名称,料号生效地是否需要添加法规审核者
 * 
 * @author laichao_sun
 *
 */

public class StatueLawAssessor {
	private static final Logger log = LogR.getLogger(StatueLawAssessor.class.getName());
	public static String path = "/ptc/Windchill/codebase/ext/tianma/part/config/statusLawAssessor.xlsx";

	// 获取法规审核者
	public static boolean setLawAssessor(WTObject object) throws WTException {
		WTPart part = null;
		Workbook we = null;
		InputStream in = null;
		try {
			if (object instanceof WTPart) {

				part = (WTPart) object;
				in = new FileInputStream(path);
				we = WorkbookFactory.create(in);
				if (null == path || path.equals("")) {
					throw new WTException("没有获取到法规审核者的配置路径，请检查!");
				}
				if (null == in || null == we) {
					throw new WTException("没有获取到法规审核者的配置路径，请检查!");
				}
				// 中英文名称
				String partCNAndENName = IbaAttributeUtil.getAttributeValue(part, "partCNAndENName");
				// 环保类别
				if (StringUtil.isNullOrEmpty(partCNAndENName)) {
					return false;
				}
				boolean bol = getLawAssessor(we.getSheetAt(0), partCNAndENName);

				return bol;

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(in);
		}

		return false;
	}

	// 获取法规审核者
	private static boolean getLawAssessor(Sheet sheet, String partCNAndENName) throws WTException {

		for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
			Row row = sheet.getRow(i);
			if (row != null) {
				String temp = getCellValue(row.getCell(0)).trim();
				if (null == temp || temp.equals("")) {
					continue;
				} else {
					if (partCNAndENName.equals(temp)) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 获取单元格中的信息
	 * 
	 * @param cell 单元格
	 * @return 主要参数信息
	 * @throws Exception
	 */
	public static String getCellValue(Cell cell) throws WTException {
		String value = "";
		try {
			if (null == cell) {
				return "";
			}
			switch (cell.getCellType()) {
			case XSSFCell.CELL_TYPE_NUMERIC:
				value = Double.toString(cell.getNumericCellValue());
				break;
			case XSSFCell.CELL_TYPE_STRING:
				value = cell.getStringCellValue();
				break;
			case XSSFCell.CELL_TYPE_BLANK:
				value = "";
				break;
			case XSSFCell.CELL_TYPE_BOOLEAN:
				value = Boolean.toString(cell.getBooleanCellValue());
				break;
			case XSSFCell.CELL_TYPE_FORMULA:
				try {
					value = String.valueOf(cell.getNumericCellValue());
				} catch (Exception e) {
					value = cell.getStringCellValue();
				}
				break;
			default:
				break;
			}
		} catch (Exception e) {
		}

		return value.trim();
	}

	/**
	 * 关闭资源
	 * 
	 * @param closeable
	 */
	public static void close(Closeable closeable) {
		try {
			if (closeable != null) {
				closeable.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
